package com.bjdd.jdy.ui.news;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjdd.anxinqiclient.dao.NewsBeanDao;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.adapter.NewsAdapter;
import com.bjdd.jdy.base.BaseFragment;
import com.bjdd.jdy.entity.NewsBean;
import com.bjdd.jdy.util.GreenDaoUtils;

import com.bjdd.jdy.util.RecyclerViewDivider;
import com.bjdd.jdy.util.StatusBarUtil;

import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultFooter;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends BaseFragment<NewsPresenter, NewsModel> implements NewsContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.rcv)
    SwipeMenuRecyclerView rcvNews;
   /* @Bind(R.id.swiperefreshlayout)
    PtrFrameLayout refreshlayout;*/
   @Bind(R.id.refreshlayout)
   SwipeRefreshLayout refreshLayout;
    private NewsAdapter adapter;
    private List<NewsBean> data;
    private NewsBeanDao dao;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(getActivity(), Color.parseColor("#F9F9F9"),0);
        toolbarBack.setVisibility(View.GONE);
        title.setText("消息");
       // initPtrFrameLayout();
        rcvNews.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context,LinearLayoutManager.VERTICAL,2, ContextCompat.getColor(context,R.color.divider_bg));
        rcvNews.addItemDecoration(divider);
       /* SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(context)
                        .setBackgroundColor(getResources().getColor(R.color.red))
                        .setText("删除")
                        .setTextColor(getResources().getColor(R.color.white))
                        .setWidth(100)
                        .setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
                swipeRightMenu.addMenuItem(deleteItem);
            }
        };
        rcvNews.setSwipeMenuCreator(swipeMenuCreator);
        rcvNews.setSwipeMenuItemClickListener(new OnSwipeMenuItemClickListener() {
            @Override
            public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) {
                if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION){
                    //右侧菜单
                    T.show("删除："+menuPosition+"---"+adapterPosition);
                    if (menuPosition == 0){

                    }
                }else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION){
                    //左侧菜单
                }
            }
        });*/
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //refreshLayout.setRefreshing(false);
                presenter.getNews();
            }
        });
        initData();
    }
    private void initData(){
        dao = GreenDaoUtils.getSingleTon().getmDaoSession().getNewsBeanDao();
        data = dao.loadAll();
        if (data == null){
            data = new ArrayList<>();
        }
        adapter = new NewsAdapter(context,data);
        rcvNews.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        refreshLayout.setRefreshing(true);
        presenter.getNews();
    }


    @Override
    public void newsBack(List<NewsBean> list) {
        data.clear();
        data.addAll(list);
        adapter.notifyDataSetChanged();
        refreshLayout.setRefreshing(false);
        dao.deleteAll();
        for (int i = 0; i < data.size(); i ++){
            dao.insert(data.get(i));
        }
    }

}
